<?php namespace App\Http\Controllers\user\uc\energy;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;
use Validator;
use Input;
use App\Models\EnterpricePowerusersBase;
use App\Http\Controllers\user\uc\CommonController;
use App\Models\Area;
use App\Models\Users;
use Session;
use App;

class GroupInfoController extends Controller {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		$uid = $this->getUid();
		$model = EnterpricePowerusersBase::with(['registProvince', 'registCity', 'registArea'])->where('uid', $uid);
		if($model->count())
		{
			$data = $model->get()->toArray()[0];
		}
		else
		{
			return App::abort(404);
		}
		return view('user.uc.energy.groupinfo',['data' => $data])->withIsEnergyInfo(true);
	}

	public function addGroupInfo()
	{
		return view("user.uc.energy.add_groupinfo")->withIsEnergyInfo(true);
	}

	public function editGroupInfo()
	{
		return view("user.uc.energy.edit_groupinfo")->withProvinces(CommonController::getAllProvince()->toArray())->withIsSaverInfo(true);
	}


	public function saveGroupInfo()
	{
		$rule = [];
		$msg = [];
		$validator = Validator::make(Input::all(), $rule, $msg);
		if($validator->passes()){
			$uid = $this->getUid();
			$model = EnterpricePowerusersBase::where("uid", $uid);
			if($model->count()){
				return App::abort(404);
			}
			$data = Input::all();
			$imgArr = Input::only("group_code_img", "regist_copy", "enterprice_pic", "logo");
			$this->moveImages($imgArr, env("OTHERS_DIR"));
			$data['uid'] = $uid;
			EnterpricePowerusersBase::create($data);
			return $this->json([]);
		}else{
			$errors = $validator->messages();
			return self::error($errors->first());
		}
	}


	public function updateGroupInfo()
	{

		$rule = array(
			// 'company_name'	=>	'required'
			);
		$msg = array();
		$validator = Validator::make(Input::all(), $rule, $msg);
		if($validator->passes()){
			$uid = $this->getUid();
			$model = EnterpricePowerusersBase::where('uid', $uid);
			if($model->count()){
				$data = Input::except('imgFile');
				$imgArr = Input::only("group_code_img", "regist_copy", "enterprice_pic", "logo");
				$dir = base_path() . "/public/images/" . $uid ."/" . env("OTHERS_DIR");
				is_dir($dir) || mkdir($dir, 0777, true);
				$path = base_path() . "/public/upload/";
				foreach ($imgArr as $key => $value) {
					$temp = $path . '/' . $value;
					$newPath = $dir . '/' . $value;
					if(is_file($temp)){
						rename($temp, $newPath);
					}
				}
				$model->update($data);
				return $this->json([]);
			}else{
				$data = Input::all();
				$data['uid'] = $uid;
				$imgArr = Input::only("group_code_img", "regist_copy", "enterprice_pic", "logo");
				$dir = base_path() . "/public/images/" . $uid ."/" . env("OTHERS_DIR");
				is_dir($dir) || mkdir($dir, 0777, true);
				$path = base_path() . "/public/upload/";
				foreach ($imgArr as $key => $value) {
					$temp = $path . '/' . $value;
					$newPath = $dir . '/' . $value;
					if(is_file($temp)){
						rename($temp, $newPath);
					}
				}
				EnterpricePowerusersBase::create($data);
				$users = Users::where("id", $uid);
				$users->role_id = env("POWERUSER");
				return $this->json([]);
			}
		}else{
			return self::error(self::DATA_IS_WRONG);
		}
	}

	public function groupInfoList()
	{
		$uid = $this->getUid();
		$model = EnterpricePowerusersBase::where('uid', $uid);
		if($model->count()){
			$data = $model->get()[0];
			return $this->json($data);
		}else{
			return self::error(self::NO_DATA);
		}

	}


	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		//
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		//
	}

	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}
}
